package com.example.memo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.memo.db.NoteHelper;
import com.example.memo.db.UserHelper;
import com.example.memo.entity.UserInfo;

public class changePW extends AppCompatActivity implements View.OnClickListener {
    private EditText et_pw_new,et_pw_ag;
    private Toolbar toolbar;
    private Button commit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pw);

        initView();
    }

    private void initView() {
        et_pw_new = findViewById(R.id.et_pw_new);
        et_pw_ag = findViewById(R.id.et_pw_ag);
        toolbar = findViewById(R.id.toolbar);
        commit = findViewById(R.id.commit);

        toolbar.setOnClickListener(this);
        commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if(view == toolbar){
            finish();
        }else if(view == commit){
            String pw_new = et_pw_new.getText().toString();
            String pw_ag = et_pw_ag.getText().toString();

            if(TextUtils.isEmpty(pw_new) || TextUtils.isEmpty(pw_ag)){
                Toast.makeText(changePW.this,"密码不能为空！",Toast.LENGTH_SHORT).show();
            }else if(!pw_new.equals(pw_ag)){
                Toast.makeText(changePW.this,"输入的密码不相同！",Toast.LENGTH_SHORT).show();
            }else {
                UserInfo userInfo = UserInfo.getUserInfo();
                UserHelper userHelper = new UserHelper(this);
                userInfo.setPassword(pw_new);
                userHelper.changePassWord(userInfo.getAccount(),pw_new);
                Toast.makeText(changePW.this,"密码修改成功，请重新登录！",Toast.LENGTH_SHORT).show();
                setResult(1);
                finish();
            }
        }
    }
}